home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 2 of 3.iso / chapter7 / getcount.c < prev    next >
C/C++ Source or Header  |  1996-03-06  |  12KB  |  341 lines

  1.  
  2. #include <windows.h>  
  3. #include <commctrl.h>
  4. #include "getcount.h"  
  5.  
  6.  
  7. #if defined (WIN32)
  8.     #define IS_WIN32 TRUE
  9. #else
  10.     #define IS_WIN32 FALSE
  11. #endif
  12.  
  13. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  14. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  15. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  16.  
  17. HINSTANCE hInst;   // current instance
  18.  
  19. LPCTSTR lpszAppName = "App";
  20. LPCTSTR lpszTitle   = "TreeView_GetCount()"; 
  21.  
  22.  
  23. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  24.  
  25.  
  26. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  27.                       LPTSTR lpCmdLine, int nCmdShow)
  28. {
  29.    MSG      msg;
  30.    HWND     hWnd; 
  31.    WNDCLASS wc;
  32.  
  33.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  34.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  35.    wc.cbClsExtra    = 0;                      
  36.    wc.cbWndExtra    = 0;                      
  37.    wc.hInstance     = hInstance;              
  38.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  39.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  40.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  41.    wc.lpszMenuName  = lpszAppName;              
  42.    wc.lpszClassName = lpszAppName;              
  43.  
  44.    if ( IS_WIN95 )
  45.    {
  46.       if ( !RegisterWin95( &wc ) )
  47.          return( FALSE );
  48.    }
  49.    else if ( !RegisterClass( &wc ) )
  50.       return( FALSE );
  51.  
  52.    hInst = hInstance; 
  53.  
  54.    hWnd = CreateWindow( lpszAppName, 
  55.                         lpszTitle,    
  56.                         WS_OVERLAPPEDWINDOW, 
  57.                         CW_USEDEFAULT, 0, 
  58.                         CW_USEDEFAULT, 0,  
  59.                         NULL,              
  60.                         NULL,              
  61.                         hInstance,         
  62.                         NULL               
  63.                       );
  64.  
  65.    if ( !hWnd ) 
  66.       return( FALSE );
  67.  
  68.    ShowWindow( hWnd, nCmdShow ); 
  69.    UpdateWindow( hWnd );         
  70.  
  71.    while( GetMessage( &msg, NULL, 0, 0) )   
  72.    {
  73.       TranslateMessage( &msg ); 
  74.       DispatchMessage( &msg );  
  75.    }
  76.  
  77.    return( msg.wParam ); 
  78. }
  79.  
  80.  
  81. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  82. {
  83.    WNDCLASSEX wcex;
  84.  
  85.    wcex.style         = lpwc->style;
  86.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  87.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  88.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  89.    wcex.hInstance     = lpwc->hInstance;
  90.    wcex.hIcon         = lpwc->hIcon;
  91.    wcex.hCursor       = lpwc->hCursor;
  92.    wcex.hbrBackground = lpwc->hbrBackground;
  93.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  94.    wcex.lpszClassName = lpwc->lpszClassName;
  95.  
  96.    // Added elements for Windows 95.
  97.    //...............................
  98.    wcex.cbSize = sizeof(WNDCLASSEX);
  99.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  100.                             IMAGE_ICON, 16, 16,
  101.                             LR_DEFAULTCOLOR );
  102.             
  103.    return RegisterClassEx( &wcex );
  104. }
  105.  
  106. VOID AddItemsToTree( HWND hTree );
  107.  
  108. #define IMAGESIZE 16
  109.  
  110. LPCTSTR lpszData[4] = { "Circle", "Rectangle", "Cross", "Check" };
  111.  
  112. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  113. {
  114. static HWND hTree = NULL;
  115.  
  116.    switch( uMsg )
  117.    {
  118.       case WM_CREATE  :
  119.               {
  120.                  HIMAGELIST hImage;
  121.  
  122.                  InitCommonControls();
  123.  
  124.                  hImage   = ImageList_Create( IMAGESIZE, IMAGESIZE, 
  125.                                               ILC_COLOR4 | ILC_MASK, 4, 1 ); 
  126.                  hTree = CreateWindowEx( WS_EX_CLIENTEDGE, WC_TREEVIEW, "",
  127.                                          WS_CHILD | WS_BORDER | WS_VISIBLE | 
  128.                                          WS_VSCROLL | TVS_HASLINES | 
  129.                                          TVS_HASBUTTONS | TVS_LINESATROOT |
  130.                                          TVS_DISABLEDRAGDROP,
  131.                                          10, 100, 100, 100,
  132.                                          hWnd,
  133.                                          (HMENU)1,
  134.                                          hInst,
  135.                                          NULL);
  136.  
  137.                  if ( hTree )
  138.                  {
  139.                     int i;
  140.  
  141.                     // Associate the image list with the tree view.
  142.                     //.............................................
  143.                     TreeView_SetImageList( hTree, hImage, TVSIL_NORMAL );
  144.  
  145.                     // Add the images to the tree view image list.
  146.                     //............................................
  147.                     for ( i=0; i<4; i++ )
  148.                        ImageList_AddIcon( hImage, LoadIcon( hInst, lpszData[i] ) );
  149.  
  150.                     AddItemsToTree( hTree );
  151.                  }
  152.               }
  153.               break;
  154.  
  155.       case WM_SIZE :
  156.               if ( hTree )
  157.                  MoveWindow( hTree, 0, 0, LOWORD( lParam ), HIWORD( lParam ), FALSE );
  158.               break;
  159.  
  160.       case WM_INITMENU :
  161.               // Gray out the menu options when they are not appropriate.
  162.               //.........................................................
  163.               if ( TreeView_GetCount( hTree ) < TreeView_GetVisibleCount( hTree ) )
  164.               {
  165.                  EnableMenuItem( (HMENU)wParam, IDM_PGUP, MF_BYCOMMAND | MF_GRAYED );
  166.                  EnableMenuItem( (HMENU)wParam, IDM_PGDOWN, MF_BYCOMMAND | MF_GRAYED );
  167.               }
  168.               else
  169.               {
  170.                  HTREEITEM hCurItem = TreeView_GetSelection( hTree );
  171.  
  172.                  EnableMenuItem( (HMENU)wParam, IDM_PGUP, MF_BYCOMMAND | 
  173.                                  ( TreeView_GetPrevVisible( hTree, hCurItem ) ?
  174.                                    MF_ENABLED : MF_GRAYED ) );
  175.  
  176.                  EnableMenuItem( (HMENU)wParam, IDM_PGDOWN, MF_BYCOMMAND | 
  177.                                  ( TreeView_GetNextVisible( hTree, hCurItem ) ?
  178.                                    MF_ENABLED : MF_GRAYED ) );
  179.               }
  180.               break;
  181.  
  182.  
  183.       case WM_COMMAND :
  184.               switch( LOWORD( wParam ) )
  185.               {
  186.                  case IDM_PGUP :
  187.                  case IDM_PGDOWN :
  188.                         {
  189.                            HTREEITEM hCurItem, hFirstVis, hNewItem;
  190.                            int       nVisCount;
  191.  
  192.                            // Retrieve the current state of the tree view.
  193.                            //.............................................
  194.                            nVisCount = TreeView_GetVisibleCount( hTree );
  195.                            hCurItem  = TreeView_GetSelection( hTree );
  196.                            hFirstVis = TreeView_GetFirstVisible( hTree );
  197.  
  198.                            // Process the page up.
  199.                            //.....................
  200.                            if ( LOWORD( wParam ) == IDM_PGUP )
  201.                            {
  202.                               if ( hCurItem != hFirstVis )
  203.                                  hNewItem = hFirstVis;
  204.                               else
  205.                               {
  206.                                  int nCnt = 0;
  207.  
  208.                                  hNewItem = hCurItem;
  209.                                  while ( nCnt < nVisCount && 
  210.                                      TreeView_GetPrevVisible( hTree, hNewItem ) )
  211.                                  {
  212.                                     hNewItem = TreeView_GetPrevVisible( hTree, hNewItem );
  213.                                     nCnt++;
  214.                                  }
  215.                               }
  216.  
  217.                               // Select the new item and make sure it is visible.
  218.                               //.................................................
  219.                               TreeView_SelectSetFirstVisible( hTree, hNewItem );
  220.                               TreeView_SelectItem( hTree, hNewItem );
  221.                            }
  222.                            // Process the Page down.
  223.                            //.......................
  224.                            else
  225.                            {
  226.                               HTREEITEM hTmp;
  227.                               int       nCnt = 1;
  228.  
  229.                               // Find the last visible item.
  230.                               //............................
  231.                               hTmp = hFirstVis;
  232.                               while( hTmp && nCnt < nVisCount && 
  233.                                      TreeView_GetNextVisible( hTree, hTmp ) )
  234.                               {
  235.                                  hTmp = TreeView_GetNextVisible( hTree, hTmp );
  236.                                  nCnt++;
  237.                               }
  238.  
  239.                               // Determine the new item.
  240.                               //........................
  241.                               if ( hTmp != hCurItem )
  242.                                  hNewItem = hTmp;
  243.                               else
  244.                               {
  245.                                  nCnt = 1;
  246.  
  247.                                  hNewItem = hCurItem;
  248.  
  249.                                  while ( nCnt < nVisCount && 
  250.                                      TreeView_GetNextVisible( hTree, hNewItem ) )
  251.                                  {
  252.                                     hNewItem = TreeView_GetNextVisible( hTree, hNewItem );
  253.                                     nCnt++;
  254.                                  }
  255.                               }
  256.  
  257.                               // Select the new item and make sure it is visible.
  258.                               //.................................................
  259.                               TreeView_EnsureVisible( hTree, hNewItem );
  260.                               TreeView_SelectItem( hTree, hNewItem );
  261.                            }
  262.                         }
  263.                         break;
  264.  
  265.                  case IDM_ABOUT :
  266.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  267.                         break;
  268.  
  269.                  case IDM_EXIT :
  270.                         DestroyWindow( hWnd );
  271.                         break;
  272.               }
  273.               break;
  274.       
  275.       case WM_DESTROY :
  276.               if ( TreeView_GetImageList( hTree, TVSIL_NORMAL ) )
  277.                  ImageList_Destroy( TreeView_GetImageList( hTree, TVSIL_NORMAL ) );
  278.  
  279.               PostQuitMessage(0);
  280.               break;
  281.  
  282.       default :
  283.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  284.    }
  285.  
  286.    return( 0L );
  287. }
  288.  
  289.  
  290. VOID AddItemsToTree( HWND hTree )
  291. {
  292.    int             i;
  293.    char            szTmp[40];
  294.    TV_INSERTSTRUCT tv;
  295.  
  296.    tv.hInsertAfter = TVI_LAST;
  297.    tv.item.mask    = TVIF_TEXT | TVIF_IMAGE | 
  298.                      TVIF_SELECTEDIMAGE | TVIF_PARAM;
  299.  
  300.    // Add the items to the tree view.
  301.    //................................
  302.    for ( i=0; i<20; i++ )
  303.    {
  304.       wsprintf( szTmp, "%s %d", lpszData[0], i+1 );
  305.  
  306.       tv.hParent      = TVI_ROOT;
  307.       tv.item.pszText = szTmp;
  308.       tv.item.iImage  = 0;
  309.       tv.item.iSelectedImage = 0;
  310.       tv.item.lParam         = 0;
  311.  
  312.       TreeView_InsertItem( hTree, &tv );
  313.    }
  314. }
  315.  
  316.  
  317. LRESULT CALLBACK About( HWND hDlg,           
  318.                         UINT message,        
  319.                         WPARAM wParam,       
  320.                         LPARAM lParam)
  321. {
  322.    switch (message) 
  323.    {
  324.        case WM_INITDIALOG: 
  325.                return (TRUE);
  326.  
  327.        case WM_COMMAND:                              
  328.                if (   LOWORD(wParam) == IDOK         
  329.                    || LOWORD(wParam) == IDCANCEL)    
  330.                {
  331.                        EndDialog(hDlg, TRUE);        
  332.                        return (TRUE);
  333.                }
  334.                break;
  335.    }
  336.  
  337.    return (FALSE); 
  338. }
  339.  
  340.  
  341.